import { defineMock } from "./base";
import type { MockRequest } from "vite-plugin-mock-dev-server";

const RECORDINGS_URL = "disturbance-records/recordings/";

// 定义录波数据类型
interface RecordingItem {
  id: number;
  name: string;
  data: string;
  Time: string;
  createTime: string;
  updateTime: string;
}

// 生成随机数据
function randomData() {
  return Math.random().toString(36).substring(2, 15);
}

// 生成单条录波数据
function generateRecording(id: number): RecordingItem {
  return {
    id,
    name: `record${id}`,
    data: randomData(),
    Time: "12:00:00",
    createTime: "2025-05-28 12:00:00",
    updateTime: "2025-05-28 12:00:00",
  };
}

// 内部数据源
let recordingList: RecordingItem[] = Array.from({ length: 100 }).map((_, id) =>
  generateRecording(id)
);

export default defineMock([
  // 列表查询
  {
    url: `${RECORDINGS_URL}list`,
    method: ["GET"],
    body: (req: MockRequest) => {
      const { page = 1, pageSize = 20 } = req.query || {};
      const start = (Number(page) - 1) * Number(pageSize);
      const end = start + Number(pageSize);
      const data = recordingList.slice(start, end);
      return {
        code: "00000",
        data: {
          list: data,
          total: recordingList.length,
        },
        msg: "ok",
      };
    },
  },
  // offset 分页查询
  {
    url: `${RECORDINGS_URL}offset-list`,
    method: ["GET"],
    body: (req: MockRequest) => {
      const { offset = 0, limit = 20, keywords = "" } = req.query || {};
      const start = offset;
      const end = start + limit;
      const data = recordingList.slice(start, end);
      return {
        code: "00000",
        data: {
          list: data,
          total: recordingList.length,
        },
        msg: "ok",
      };
    },
  },
  // 批量删除
  {
    url: `${RECORDINGS_URL}batch-delete`,
    method: ["POST"],
    body: (req: MockRequest) => {
      const { ids } = req.body || {};
      // 参数校验
      if (!Array.isArray(ids) || ids.length === 0) {
        return {
          code: "10001",
          msg: "参数错误：ids必须为非空数组",
        };
      }
      recordingList = recordingList.filter((item) => !ids.includes("" + item.id));
      return { code: "00000", data: null, msg: `已删除${ids.length}项` };
    },
  },
  // 批量更新
  {
    url: `${RECORDINGS_URL}batch-update`,
    method: ["POST"],
    body: (req: MockRequest) => {
      const { updates } = req.body || {};
      if (!Array.isArray(updates)) {
        return { code: "10001", msg: "参数错误，缺少updates数组" };
      }
      let updateCount = 0;
      updates.forEach((update: Partial<RecordingItem> & { id: number }) => {
        const idx = recordingList.findIndex((item) => item.id === update.id);
        if (idx !== -1) {
          recordingList[idx] = {
            ...recordingList[idx],
            ...update,
            updateTime: new Date().toISOString(),
          };
          updateCount++;
        }
      });
      return { code: "00000", data: null, msg: `已更新${updateCount}项` };
    },
  },
  // /device/disturbance-records/recordings
  {
    url: `device/disturbance-records/recordings/offset-list`,
    method: ["GET"],
    body: (req: MockRequest) => {
      const { offset = 0, limit = 20, keywords = "" } = req.query || {};
      const start = offset;
      const end = start + limit;
      const data = recordingList.slice(start, end);
      return {
        code: "00000",
        data: {
          list: data,
          total: recordingList.length,
        },
        msg: "ok",
      };
    },
  },
  {
    url: `device/disturbance-records/recordings/batch-delete`,
    method: ["POST"],
    body: (req: MockRequest) => {
      const { ids } = req.body || {};
      // 参数校验
      if (!Array.isArray(ids) || ids.length === 0) {
        return {
          code: "10001",
          msg: "参数错误：ids必须为非空数组",
        };
      }
      recordingList = recordingList.filter((item) => !ids.includes("" + item.id));
      return { code: "00000", data: null, msg: `已删除${ids.length}项` };
    },
  },
]);
